#!/usr/bin/python3
# -*- coding: UTF-8 -*-

# Copyright (C) 2025 Huawei Technologies Co., Ltd. All rights reserved.

"""
Created on 2013-3-26

"""
import os
import re
import sys
import pwd
import grp
import glob
import shutil
import inspect
import logging
import subprocess
import argparse

logging.basicConfig(format="%(message)s")

G_CMK_USER = "prod"
G_CMK_GROUP = "prod"
G_NAGIOS_USER = "nagios"
G_NAGIOS_GROUP = "nagios"
thisFile = inspect.getfile(inspect.currentframe())
fileDir, fileName = os.path.split(thisFile)
if fileDir == "":
    fileDir = os.getcwd()
usrFile = fileDir + os.path.sep + "usrFile.cfg"


def getUsrInfo():
    strlist = []
    usr = "prod"
    group = "prod"
    file = None
    try:
        file = open(usrFile, "r+")
        strlist = file.readlines()
    except Exception as e:
        logging.error("error : open usrFile.cfg error : " + str(e))
    finally:
        if file is not None:
            file.close()
    if not strlist:
        return usr, group
    for eachline in strlist:
        if re.findall(r"usr.*=.*", eachline):
            usr = eachline.split("=")[1]
        if re.findall(r"group.*=.*", eachline):
            group = eachline.split("=")[1]
    return usr.strip(), group.strip()


def getNagiosUserinfo():
    try:
        st = os.stat("/usr/local/nagios")
        usr = pwd.getpwuid(st.st_uid).pw_name
        group = grp.getgrgid(st.st_gid).gr_name
        return usr, group
    except Exception as e:
        logging.error("getNagiosUserinfo error: " + str(e))
        return "nagios", "nagios"


"""
==================================
日志
==================================
"""

##兼容的check mk 版本
CHECK_MK_VERSION_LIST = ["1_2", "1_5", "1_4"]


class Log(logging.Logger):
    """轻量封装标准库 Logger，兼容原有调用方式。"""

    def __init__(self, classname=None):
        name = str(classname) if classname is not None else __name__
        super().__init__(name)
        # map TargetPath.LOG_LEVEL to std logging levels
        level_map = {
            "error": logging.ERROR,
            "info": logging.INFO,
            "debug": logging.DEBUG,
        }
        self.setLevel(logging.INFO)
        self._prefix = (str(classname) + "=> ") if classname is not None else ""
        # avoid duplicate logs; add simple stream handler if none exists
        if not self.handlers:
            handler = logging.StreamHandler()
            handler.setFormatter(logging.Formatter("%(message)s"))
            self.addHandler(handler)
        self.propagate = False

    def error(self, textContent=""):
        super().error(self._prefix + str(textContent))

    def info(self, textContent=""):
        super().info(self._prefix + str(textContent))

    def debug(self, textContent=""):
        super().debug(self._prefix + str(textContent))


"""
'==================================
'进行配置
'==================================
"""


class CheckCfgTask:
    LOG = Log("check_cfg_task.py")

    """
    '配置是否有误
    """

    @staticmethod
    def checkNagiosCfg(command):
        return os.system(command)

    """
    '配置路径
    """

    def checkPath(self, pathStr):
        result = os.path.isdir(pathStr) or os.path.isfile(pathStr) or os.path.exists(pathStr)
        if not result:
            self.LOG.error(
                "checkpath:" + pathStr + (result and "  sucess." or "  fail.")
            )
        return result

    """
    '判断是否编辑
    """

    def isWrite(self, pathStr):
        try:
            with open(pathStr, "a+"):
                return True
        except Exception:
            self.LOG.info("check:" + pathStr + " fail.")
            return False


# store ckMKVersion
class CKmkVersion:
    __ckmkVersion = ""

    @classmethod
    def setckmkVersion(cls, version):
        cls.__ckmkVersion = version

    @classmethod
    def getckmkVersion(cls):
        return cls.__ckmkVersion

    @classmethod
    def find_ckmkVersion(cls):
        """return nagios directory"""
        return os.environ.get("NAGIOS_CHECKMK_VERSION", "")


def findNagiosDir():
    """return nagios directory"""
    return os.environ.get("NAGIOSHOME", "/usr/local/nagios")


def delckmkVersionToprofile():
    FilePath = "/etc/profile"
    strforconfig = "export NAGIOS_CHECKMK_VERSION"
    file = None
    try:
        file = open(FilePath, "r+")
        strList = file.readlines()
        file.close()
        # had configed so exit
        for tmpLine in strList:
            if strforconfig in tmpLine:
                del tmpLine
                break
        # cofnig nagios.cfg
        file = open(FilePath, "w")
        file.writelines(strList)
    except Exception as err:
        logging.error("delNagiosHomeToprofile exception info: " + str(err))
    finally:
        if file is not None:
            file.close()


def addckmkVersionToprofile():
    FilePath = "/etc/profile"
    strforconfig = "export NAGIOS_CHECKMK_VERSION="
    file = None
    delckmkVersionToprofile()
    try:
        file = open(FilePath, "r+")
        strlist = file.readlines()
        file.writelines(strforconfig + ("%s" % CKmkVersion.getckmkVersion()) + "\n")

    except Exception as err:
        logging.error("addckmkVersionToprofile exception info :" + str(err))
    finally:
        if file is not None:
            file.close()


"""
'==================================
'查找匹配移动文件
'==================================
"""


class FileMrg:
    LOG = Log("file_mrg.py")
    """
    '读取文件
    'regFindContent  选择插入的位置
    'regPtContent    完全匹配textContent
    """

    @staticmethod
    def excludeFile(filetarget):
        return not os.path.isfile(filetarget)

    """
    '内容匹配
    """

    @staticmethod
    def contentPattern(reg, content):
        if reg.search(content):
            return True
        return False

    def readEditFile(
        self,
        nagiosCfgPath,
        textContent,
        regIndexContent=None,
        regPtContent=None,
        mode=True,
    ):
        """
        '如果更改的文件内容不是一个路径
        """
        if not os.path.isfile(nagiosCfgPath):
            return

        nagiosCfgPathBak = nagiosCfgPath + ".bak"
        insertContent = textContent

        if regIndexContent is None:
            regIndexContent = r"^\s*" + insertContent
        if regPtContent is None:
            regPtContent = r"^\s*%s\s*$" % insertContent
        patt = re.compile(regIndexContent)
        patt2 = re.compile(regPtContent)

        cfgCheck = True

        f2 = None
        f = None
        try:
            try:
                f2 = open(nagiosCfgPathBak, "w")
                f = open(nagiosCfgPath, "r")
                for fContent in f:
                    if cfgCheck and self.contentPattern(patt, fContent):
                        cfgCheck = False
                        if not mode and self.contentPattern(patt2, fContent):
                            continue
                        f2.write(insertContent + "\n")

                    if self.contentPattern(patt2, fContent):
                        continue

                    f2.write(fContent)
            except IOError:
                self.LOG.error('"readEditFile" Except! Permission denied.')
                return
            else:
                self.LOG.debug(
                    '"readEditFile" update file content:' + str(nagiosCfgPath)
                )
        finally:
            if f is not None:
                f.close()
            if f2 is not None:
                f2.close()

        _ = self.withFileContent(nagiosCfgPathBak, nagiosCfgPath)
        self.removeFile(nagiosCfgPathBak)

    """
    '使用文件替换另一文件
    """

    def withFileContent(self, source, target):
        f2 = None
        f = None
        try:
            try:
                f2 = open(target, "w")
                f = open(source, "r")
                f2.write(f.read())
            except IOError:
                self.LOG.error('"withFileContent" Except! Permission denied.')
                return False
            else:
                self.LOG.debug('"withFileContent" withFileContent: success.')
                return True
        finally:
            f2.close()
            f.close()

    """
    '移动文件
    """

    def copyFile(self, filesouce, filetarget):
        if os.path.isfile(filesouce) and self.excludeFile(
            filetarget + os.path.sep + os.path.basename(filesouce)
        ):
            shutil.copy2(filesouce, os.path.join(filetarget))
        elif os.path.isdir(filesouce):
            dirname = os.path.basename(filesouce)
            if not os.path.isdir(os.path.join(filetarget) + os.path.sep + dirname):
                os.mkdir(os.path.join(filetarget) + os.path.sep + dirname)
            for infile in glob.glob(os.path.join(filesouce, "*")):
                self.copyFile(infile, os.path.join(filetarget) + os.path.sep + dirname)

    """
    '移除文件
    """

    def removeFile(self, filepath, excludeReg=None):
        filepath = os.path.join(filepath)
        if excludeReg is not None and re.findall(excludeReg, filepath):
            return
        if os.path.isfile(filepath):
            os.remove(filepath)
        elif os.path.isdir(filepath):
            if len(os.listdir(filepath)) != 0:
                for infile in glob.glob(os.path.join(filepath, "*")):
                    self.removeFile(infile, excludeReg)
            try:
                os.rmdir(filepath)
            except OSError:
                self.LOG.debug("Failed to remove dir")

    """
    '写文件
    """

    def createFile(self, filepath, filename, textContent, mode="w"):
        if os.path.isdir(filepath):
            f = None
            try:
                try:
                    f = open(filepath + os.path.sep + filename, mode)
                    f.write(textContent)
                    f.flush()
                except IOError:
                    self.LOG.error('"createFile" Except! Permission denied.')
            finally:
                if f is not None:
                    f.close()


"""
'==================================
'配置信息
'==================================    
"""


class TargetPath(object):
    """
    '当前系统的nagios_home路径
    """

    NAGIOS_HOME = "/usr/local/nagios"

    """
    '配置etc目录
    """

    def nagios_etc(self):
        return "%s/etc" % self.NAGIOS_HOME

    """
    '配置libexec目录
    """

    def nagios_libexec(self):
        return "%s/libexec" % self.NAGIOS_HOME

    """
    '配置bin目录
    """

    def nagios_bin(self):
        return "%s/bin" % self.NAGIOS_HOME

    """
    '配置nagios.config文件
    """

    def nagios_f_config(self):
        return "%s/nagios.cfg" % self.nagios_etc()

    """
    '配置cgi.config文件
    """

    def nagios_cgi_config(self):
        return "%s/cgi.cfg" % self.nagios_etc()

    """
    '当前nagios运行文件位置
    """

    def nagios_server(self):
        return "%s/nagios" % self.nagios_bin()

    """
    'nagios可执行文件目录 
    'update 2013-4-8
    """

    def nagios_var(self):
        return "%s/var" % self.NAGIOS_HOME

    """
    'nagios数据文件
    'update 2013-4-8
    """
    NAGIOS_CMD = ""

    def nagios_cmd(self):
        if (
            CKmkVersion.getckmkVersion() == "1_4"
            or CKmkVersion.getckmkVersion() == "1_5"
        ):
            return "/omd/sites/%s/tmp/run/nagios.cmd" % G_CMK_USER
        return self.NAGIOS_CMD or "%s/rw/nagios.cmd" % self.nagios_var()

    """
    'nagios缓存目录
    'update 2013-4-8
    """
    NAGIOS_CACHE_PATH = ""

    def nagios_cache_path(self):
        return self.NAGIOS_CACHE_PATH or "%s/openubmc" % self.nagios_var()

    """
    'nagios端口号
    'update 2013-4-8
    """
    NAGIOS_LISTEN_PORT = 10061

    """
    'nagios本机地址
    'update 2013-4-8
    """
    NAGIOS_LOCAL_ADDRESS = "127.0.0.1"

    """
    '当前模板位置
    """

    def nagios_config(self):
        return "%s/openubmc/templates/server.cfg.template" % self.nagios_etc()

    def nagios_command(self):
        return "%s/openubmc/templates/command.cfg.template" % self.nagios_etc()

    def nagios_ser_cfg(self):
        return "%s/openubmc/server.cfg" % self.nagios_etc()

    def nagios_listener(self):
        return "%s/openubmc/listener.cfg" % self.nagios_etc()

    """
    '当前文件路径
    """
    CURR_PATH = fileDir

    """
    '日志级别
    ' error   错误
    ' info    信息
    ' debug   调试
    """
    LOG_LEVEL = "info"


class InstallExcept(Exception):
    pass


class HelpExcept(Exception):
    pass


"""
'初始化用户配置
"""


class InitUserCfg(object):
    def initHwServer(self):
        initHwSer = """<?xml version="1.0" encoding="UTF-8"?>
<hosts>
        <host>
        <!--HOST基本信息(设备型号：Rack/HighDensity/E9000)-->
        <device>
            <hostname></hostname>
            <ipaddress></ipaddress>
            <devicetype></devicetype>
            <port>161</port>
            <user></user>
            <pass></pass>
            <authprotocol></authprotocol>
            <privprotocol></privprotocol>
        </device>
        <!--设备数据采集协议：
            1、采集数据的SNMP版本为V3，则告警上报支持V1/V2/V3；
            2、采集数据的SNMP版本为V1/V2，则告警上报只支持V1/V2
            3、E9000采集数据只支持SNMP V3版本
        -->
        <collect>
            <snmpversion></snmpversion>
            <community></community>            
        </collect>
        
        <!--设备告警上报的协议信息：
            1、E9000告警上报只支持SNMP V1/V2版本。-->
        <alarm>
            <trapsnmpversion></trapsnmpversion>
            <trapcommunity></trapcommunity>
        </alarm>
    </host>
    

</hosts>
"""
        fileMrg.createFile(os.path.join(targetPath.CURR_PATH, "etc", "openubmc"), "hosts.xml", initHwSer, "w")

    def initOpenubmcHosts(self):
        fileMrg.removeFile(os.path.join(targetPath.CURR_PATH, "etc", "openubmc", "server.cfg"))

    def initCfg(self):
        self.initHwServer()
        self.initOpenubmcHosts()


"""
'初始化对象
"""
targetPath = TargetPath
fileMrg = FileMrg()
checkCfgTask = CheckCfgTask()

"""
'定义变量
"""
LOG = Log("setup.py")


"""
'读取路径配置文件
"""

##如果配置文件将在这里编写


"""
'路径的正确
"""


def checkpath():
    if "1_4" in CKmkVersion.getckmkVersion() or "1_5" in CKmkVersion.getckmkVersion():
        return True
    pathsToCheck = (
        targetPath.NAGIOS_HOME,
        targetPath().nagios_bin(),
        targetPath().nagios_etc(),
        targetPath().nagios_libexec(),
        targetPath().nagios_f_config()
        # 更多的文件校验
    )
    if (
        any(not checkCfgTask.checkPath(tmpPath) for tmpPath in pathsToCheck)
        or not checkCfgTask.isWrite(targetPath().nagios_f_config())
        or not checkCfgTask.isWrite(targetPath().nagios_cgi_config())
    ):
        raise InstallExcept("Incorrect nagios installation path. ")
    else:
        ########
        # 创建缓存目录
        ########
        if not os.path.isdir(targetPath().nagios_cache_path()):
            os.makedirs(targetPath().nagios_cache_path(), 0o775)
        return True


def createPluginDir():
    if not os.path.isdir(targetPath.NAGIOS_HOME):
        os.mkdir(targetPath.NAGIOS_HOME)
    if not os.path.isdir(targetPath().nagios_etc()):
        os.mkdir(targetPath().nagios_etc())
    if not os.path.isdir(targetPath().nagios_libexec()):
        os.mkdir(targetPath().nagios_libexec())
    if not os.path.isdir(targetPath().nagios_bin()):
        os.mkdir(targetPath().nagios_bin())
    if not os.path.isdir(targetPath().nagios_var()):
        os.mkdir(targetPath().nagios_var())
    if not os.path.isdir(os.path.join(targetPath().nagios_var(), "openubmc")):
        os.mkdir(os.path.join(targetPath().nagios_var(), "openubmc"))


"""
'移动python脚本至指定位
"""


def moveFile():
    createPluginDir()
    LOG.info("moveFile begin.")
    fileMrg.copyFile(
        targetPath.CURR_PATH + os.path.sep + "bin" + os.path.sep + "openubmc",
        targetPath().nagios_bin(),
    )
    fileMrg.copyFile(
        targetPath.CURR_PATH + os.path.sep + "etc" + os.path.sep + "openubmc",
        targetPath().nagios_etc(),
    )
    fileMrg.copyFile(
        targetPath.CURR_PATH + os.path.sep + "libexec" + os.path.sep + "openubmc",
        targetPath().nagios_libexec(),
    )
    LOG.info("moveFile end.")


def removeFile(isSaveData):
    LOG.info("removeFile begin.")
    userData = None
    if isSaveData:
        userData = "hosts.xml|server.cfg"
    fileMrg.removeFile(targetPath().nagios_bin() + os.path.sep + "openubmc")
    fileMrg.removeFile(targetPath().nagios_etc() + os.path.sep + "openubmc", userData)
    fileMrg.removeFile(targetPath().nagios_libexec() + os.path.sep + "openubmc")
    fileMrg.removeFile(targetPath().nagios_cache_path())
    LOG.info("removeFile end.")


"""
'修改nagios配置文件 
"""


def delInfoInNagiosSourceFile():
    nagioshome = targetPath.NAGIOS_HOME
    if "1_4" in CKmkVersion.getckmkVersion() or "1_5" in CKmkVersion.getckmkVersion():
        FilePath = "/omd/sites/%s/etc/nagios/resource.cfg" % (G_CMK_USER)
    else:
        FilePath = SourceFile = (
            nagioshome + os.path.sep + "etc" + os.path.sep + "resource.cfg"
        )
    strforconfig = "$USER5$=%s/libexec\n" % nagioshome
    file = None
    try:
        file = open(FilePath, "r+")
        strList = file.readlines()
        file.close()
        # had configed so exit
        for tmpLine in strList:
            if strforconfig in tmpLine:
                del tmpLine
                break
        # cofnig nagios.cfg
        file = open(FilePath, "w")
        file.writelines(strList)
    except Exception as err:
        logging.error("delInfoInNagiosSourceFile exception info: " + str(err))
    finally:
        if file is not None:
            file.close()


def delInfoInNagiosCfg():
    nagioshome = targetPath.NAGIOS_HOME
    if "1_4" in CKmkVersion.getckmkVersion() or "1_5" in CKmkVersion.getckmkVersion():
        FilePath = "/omd/sites/%s/etc/nagios/nagios.cfg" % (G_CMK_USER)
        strforconfig = (
            "precached_object_file=%s" % nagioshome
            + os.path.sep
            + "etc"
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "server.cfg\n"
        )
    else:
        FilePath = nagioshome + os.path.sep + "etc" + os.path.sep + "nagios.cfg"
        strforconfig = (
            "cfg_file=%s" % nagioshome
            + os.path.sep
            + "etc"
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "server.cfg\n"
        )
    file = None
    try:
        file = open(FilePath, "r+")
        strList = file.readlines()
        file.close()
        # had configed so exit
        for tmpLine in strList:
            if strforconfig in tmpLine:
                del tmpLine
                break
        # cofnig nagios.cfg
        file = open(FilePath, "w")
        file.writelines(strList)
    except Exception as err:
        logging.error("delInfoInNagiosCfg exception info :" + str(err))
    finally:
        if file is not None:
            file.close()


def reEditFile():
    LOG.info("revert file begin.")

    cfg_pt = "cfg_file="
    cfg = cfg_pt + targetPath().nagios_ser_cfg()
    cfg_pt = r"^\s*%s\s*$" % cfg
    fileMrg.readEditFile(targetPath().nagios_f_config(), cfg, cfg_pt, mode=False)

    LOG.info("revert file end.")


"""
'配置文件正确
"""


def checkNagiosCfg():
    LOG.info("checkNagiosCfg begin.")
    command = targetPath().nagios_server() + " -v " + targetPath().nagios_f_config()
    checkres = checkCfgTask.checkNagiosCfg(command)
    LOG.info("checkNagiosCfg end.")
    return checkres


SERVER_CFG = "server.cfg"
COMMAND_CFG = "command.cfg"
currentfilepath = (
    os.path.abspath(os.path.dirname(thisFile))
    + os.path.sep
    + "etc"
    + os.path.sep
    + "openubmc"
)


def insertInitialCfg():
    initial_content = (
        """[init]
nagios_dir="""
        + targetPath.NAGIOS_HOME
        + """
nagios_cmd_file="""
        + targetPath().nagios_cmd()
        + """
cache_path="""
        + targetPath().nagios_cache_path()
        + """
local_address="""
        + targetPath().NAGIOS_LOCAL_ADDRESS
        + """
listen_port="""
        + targetPath().NAGIOS_LISTEN_PORT
        + """
check_host_cmd=ping %s -c 1 > /dev/null
check_host_interval = 300.0
check_nagios_cmd=ps -efww | grep nagios | grep -v grep | awk '{print $8}' | grep %s > /dev/null
check_nagios_interval = 120.0
nagios_costant3=awei12
"""
    )
    fileMrg.createFile(os.path.join(targetPath.CURR_PATH, "etc", "openubmc"), "initial.cfg", initial_content, "w")


# verify python vesion is >= 3.8; if yes return python executable path
def VerifyPythonVersion():
    try:
        if (
            sys.version_info[0] == 3
            and sys.version_info[1] >= 8
        ):
            PythonPath = sys.executable
            logging.error(os.path.dirname(PythonPath))
            return os.path.dirname(PythonPath)
        else:
            logging.error("check python vesion fail, please check you python is >= 3.8")
            return ""
    except Exception:
        logging.error("check python vesion fail, please check you python is >= 3.8")
        return ""


def trapdcheck():
    PythonExcutePath = VerifyPythonVersion()
    if not PythonExcutePath:
        raise Exception("check python vesion fail, please check you python is >= 3.8")
    trapdCheckStr = (
        """#/bin/bash

trapdNum=NULL
function getTrapdNum()
{
     trapdNum=`ps -efww | grep trapd.py|grep python | grep -v grep | awk '{print $2}'`
}

collectNum=NULL
function getCollectNum()
{
     collectNum=`ps -efww | grep collect.py|grep python | grep -v grep | awk '{print $2}'`
}
CollectbyCfgNum=NULL
function getCollectbyCfgNum()
{
     CollectbyCfgNum=`ps -efww | grep collectInfoByCfg.py|grep python | grep -v grep | awk '{print $2}'`
}

function kill()
{
    killNum=`kill -9 $1`; 
}

function startTrapd()
{

    cd `dirname $0`
    nohup python """
        + targetPath().nagios_bin()
        + os.path.sep
        + """openubmc"""
        + os.path.sep
        + """trapd.py > /dev/null 2>&1 &
}

function startCollect()
{

    cd `dirname $0`
    nohup python """
        + targetPath().nagios_bin()
        + os.path.sep
        + """openubmc"""
        + os.path.sep
        + """collect.py -p > /dev/null 2>&1 &
}

function startCollectByCfg()
{

    cd `dirname $0`
    nohup python """
        + targetPath().nagios_bin()
        + os.path.sep
        + """openubmc"""
        + os.path.sep
        + """collectInfoByCfg.py -p > /dev/null 2>&1 &
}

function trapdcheck()
{
    getTrapdNum
    if [ -e $trapdNum ]; then
        startTrapd
    fi

    getCollectNum
    if [ -e $collectNum ]; then
        startCollect
    fi

    getCollectbyCfgNum
    if [ -e $CollectbyCfgNum ]; then
        startCollectByCfg
    fi

    echo "OK"
    exit 0
}
source /etc/profile
export PATH="%s:$PATH"
export PYTHONPATH=''
trapdcheck
"""
        % PythonExcutePath
    )
    fileMrg.createFile(os.path.join(targetPath.CURR_PATH, "libexec", "openubmc"), "trapdcheck.sh", trapdCheckStr, "w")

    # 修改启动脚本为nohup python /usr/local/nagios/bin/openubmc/trapd.py > /dev/null &
    # 这样在trapd.py所在目录就不会出现nohup.out文件


def checkip(ip):
    if re.findall(
        "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}"
        + "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$",
        ip,
    ):
        pass
    else:
        LOG.error("Incorrect IP address format.")
        raise InstallExcept()


def checkSnmpversion(snmpversion):
    if "v1" == snmpversion or "v2c" == snmpversion or "v3" == snmpversion:
        pass
    else:
        LOG.error("Incorrect Snmp Version format.")
        raise InstallExcept()


def encrypt(inputStr):
    """
    ' aes加密，依赖linux平台，依赖openssl
    """
    encryptStr = (
        os.popen("echo " + "'" + inputStr + "'" + " | openssl aes-128-cbc -k '' -base64")
        .read()
        .strip()
    )
    if encryptStr is None or encryptStr == "":
        return inputStr
    return encryptStr


def delNagiosHomeToprofile():
    FilePath = "/etc/profile"
    strforconfig = "export NAGIOSHOME="
    file = None
    try:
        file = open(FilePath, "r+")
        strList = file.readlines()
        file.close()
        # had configed so exit
        for tmpLine in strList:
            if strforconfig in tmpLine:
                del tmpLine
                break
        # cofnig   nagios.cfg
        file = open(FilePath, "w")
        file.writelines(strList)
    except Exception as err:
        logging("delNagiosHomeToprofile exception info :" + str(err))
    finally:
        if file is not None:
            file.close()


def addNagiosHomeToprofile():
    FilePath = "/etc/profile"
    strforconfig = "export NAGIOSHOME=%s" % targetPath.NAGIOS_HOME
    file = None
    delNagiosHomeToprofile()
    try:
        file = open(FilePath, "r+")
        strlist = file.readlines()

        file.writelines(strforconfig + "\n")

    except Exception as err:
        logging.error("addNagiosHomeToprofile exception info :" + str(err))
    finally:
        if file is not None:
            file.close()


# add config in nagios
def reuseConfigfile():
    cfgfile = targetPath().nagios_ser_cfg()
    configBinPath = os.path.join(targetPath().nagios_bin(), "openubmc", "config.py")
    if os.path.isfile(cfgfile):
        tmpcmd = "python %s update" % configBinPath
        os.system(tmpcmd)


def install():

    # clear env before install - kill trapd.py and collect.py safely
    try:
        procs = subprocess.run(["/usr/bin/ps", "-ef"], shell=False, text=True, capture_output=True)
        for line in procs.stdout.splitlines():
            if "grep" in line:
                continue
            if "python" in line and ("trapd.py" in line or "collect.py" in line):
                parts = line.split()
                if len(parts) < 2:
                    continue
                pid_str = parts[1]
                try:
                    os.kill(int(pid_str), 9)
                except Exception:
                    LOG.debug("Failed to kill trap.py or collect.py")
    except Exception:
        LOG.debug("Failed to get trap.py or collect.py")

    if ("__main__" == __name__) and checkpath():
        if checkCfgTask.checkPath(os.path.join(targetPath().nagios_bin(), "openubmc", "trapd.py")):
            LOG.error("Plug-in registrationed. Please uninstalled, try install.")
            raise InstallExcept()

        trapdcheck()
        """
        '自动创建nagios用户目录
        """
        try:
            os.makedirs("/home/nagios", 0o775, exist_ok=True)
        except OSError:
            logging.warning("Failed to make nagios dir")

        """
        '初始化
        """
        # 做一些初始化工作
        # 更新initial.cfg文件 begin

        insertInitialCfg()
        # 更新initial.cfg文件 end

        """
        '移动python脚本至指定位
        """

        moveFile()
        os.system(
            "chmod -R 700 " + targetPath().nagios_var() + os.path.sep + "openubmc"
        )
        os.system(
            "chmod -R 700 " + targetPath().nagios_bin() + os.path.sep + "openubmc"
        )
        os.system(
            "chmod -R 700 " + targetPath().nagios_libexec() + os.path.sep + "openubmc"
        )
        os.system(
            "chmod -R 700 " + targetPath().nagios_etc() + os.path.sep + "openubmc"
        )

        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "configInfo.cfg"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "hosts.xml"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "initial.cfg"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "nagios_plugin.cfg"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "pluginConfig.xml"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "device.xml"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "device"
            + os.path.sep
            + "High-density_Common.xml"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_etc()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "device"
            + os.path.sep
            + "Rack_Common.xml"
        )

        os.system(
            "chmod 600 "
            + targetPath().nagios_bin()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "constInfo.py"
        )
        os.system(
            "chmod 600 "
            + targetPath().nagios_bin()
            + os.path.sep
            + "openubmc"
            + os.path.sep
            + "dataInfo.py"
        )
        if (
            CKmkVersion.getckmkVersion() == "1_4"
            or CKmkVersion.getckmkVersion() == "1_5"
        ):
            _currenPath = os.getcwd()
            copyusrfilecmd = "cp '%s'/usrFile.cfg '%s'" % (
                _currenPath,
                targetPath().nagios_etc() + os.path.sep + "openubmc",
            )
            os.system(copyusrfilecmd)
            os.system(
                "chown -R %s:%s %s" % (G_CMK_USER, G_CMK_GROUP, targetPath.NAGIOS_HOME)
            )

        else:
            os.system(
                "chown -R %s:%s " % (G_NAGIOS_USER, G_NAGIOS_GROUP)
                + targetPath().nagios_bin()
                + os.path.sep
                + "openubmc"
            )
            os.system(
                "chown -R %s:%s " % (G_NAGIOS_USER, G_NAGIOS_GROUP)
                + targetPath().nagios_etc()
                + os.path.sep
                + "openubmc"
            )
            os.system(
                "chown -R %s:%s " % (G_NAGIOS_USER, G_NAGIOS_GROUP)
                + targetPath().nagios_libexec()
                + os.path.sep
                + "openubmc"
            )
            os.system(
                "chown -R %s:%s " % (G_NAGIOS_USER, G_NAGIOS_GROUP)
                + targetPath().nagios_var()
                + os.path.sep
                + "openubmc"
            )
        # set ENV
        addNagiosHomeToprofile()
        addckmkVersionToprofile()
        reuseConfigfile()


def uninstall(isRetain):
    #
    if isRetain.upper() == "YES":
        isSaveData = True
    else:
        isSaveData = False

    delNagiosHomeToprofile()
    delckmkVersionToprofile()

    if checkpath():
        try:
            procs = subprocess.run(["/usr/bin/ps", "-ef"], shell=False, text=True, capture_output=True)
            for line in procs.stdout.splitlines():
                if "grep" in line:
                    continue
                if "python" in line and ("trapd.py" in line or "collect.py" in line):
                    parts = line.split()
                    if len(parts) < 2:
                        continue
                    pid_str = parts[1]
                    try:
                        os.kill(int(pid_str), 9)
                    except Exception:
                        LOG.debug("Failed to kill trap.py or collect.py")
        except Exception:
            LOG.debug("Failed to kill trap.py or collect.py")

        removeFile(isSaveData)
        reEditFile()
        delInfoInNagiosCfg()
        delInfoInNagiosSourceFile()

        """
        '重启nagios服务
        """
        if (
            "1_4" in CKmkVersion.getckmkVersion()
            or "1_5" in CKmkVersion.getckmkVersion()
        ):
            os.system("/usr/bin/omd restart")
        else:
            os.system("/usr/sbin/service nagios restart")


if __name__ == "__main__":
    InitUserCfg().initCfg()

    MSG_USAGE = """\n    setup.py install -d LOCAL_ADDRESS [-p LISTEN_PORT] -n NAGIOS_DIR \
                   \nor\
                   \n    setup.py uninstall -n NAGIOS_DIR 
                """
    parser = argparse.ArgumentParser(usage=MSG_USAGE)
    parser.add_argument(
        "-d",
        "--local_address",
        action="store",
        dest="local_address",
        help="Local IP address (mandatory). example: 192.168.1.1",
    )

    parser.add_argument(
        "-p",
        "--listen_port",
        action="store",
        dest="listen_port",
        default="10061",
        help="Listening port number. Default value: 10061",
    )

    parser.add_argument(
        "-n",
        "--nagios_dir",
        action="store",
        dest="nagios_dir",
        help="Nagios home directory (mandatory). Example:/usr/local/nagios",
    )
    parser.add_argument(
        "-m",
        "--checkMKVersion",
        action="store",
        dest="checkMKVersion",
        help="assign checkMK version. Example: 1_4 for checkMK1.4 ",
    )
    parser.add_argument(
        "-s",
        "--retain",
        action="store",
        dest="retain",
        help="retain user date yes for save date. Example: 'yes', 'no' ",
    )
    parser.add_argument("command", nargs="?")

    options = parser.parse_args()

    # verify -m
    if options.checkMKVersion is not None:
        if options.checkMKVersion not in CHECK_MK_VERSION_LIST:
            logging.error("-m only suport as follow:")
            for vesionitem in CHECK_MK_VERSION_LIST:
                logging.error(vesionitem)

    if options.command in ("install", "uninstall"):
        if options.command == "uninstall":
            if options.nagios_dir is None:
                logging.error("please input nagios dir ")
                exit()
            if options.checkMKVersion is not None:
                CKmkVersion.setckmkVersion(options.checkMKVersion)
            else:
                CKmkVersion.setckmkVersion(CKmkVersion.find_ckmkVersion())
            try:
                if (
                    "1_4" in CKmkVersion.getckmkVersion()
                    or "1_5" in CKmkVersion.getckmkVersion()
                ):
                    targetPath.NAGIOS_HOME = (
                        re.sub(r"[/|\\]*$", "", options.nagios_dir)
                        + "/"
                        + "nagiosPlugInForOpenubmc"
                    )
                    G_CMK_USER, G_CMK_GROUP = getUsrInfo()
                else:
                    G_NAGIOS_USER, G_NAGIOS_GROUP = getNagiosUserinfo()
                    targetPath.NAGIOS_HOME = re.sub(r"[/|\\]*$", "", options.nagios_dir)
                if options.retain is not None:
                    uninstall(options.retain)
                else:
                    uninstall("no")
            except HelpExcept:
                parser.print_help()
            except InstallExcept as exc:
                LOG.error(str(exc.message) + "uninstall fail.")
                logging.error("Done.")
            else:
                LOG.info("uninstall success.")
                logging.error("Done.")
        elif options.command == "install":
            if options.local_address is not None and options.nagios_dir is not None:
                try:
                    checkip(options.local_address)
                    targetPath.NAGIOS_LOCAL_ADDRESS = options.local_address
                    targetPath.NAGIOS_HOME = re.sub(r"[/|\\]*$", "", options.nagios_dir)

                    if options.listen_port is not None:
                        targetPath.NAGIOS_LISTEN_PORT = options.listen_port
                    if options.checkMKVersion is not None:
                        CKmkVersion.setckmkVersion(options.checkMKVersion)
                        if (
                            "1_4" in options.checkMKVersion
                            or "1_5" in options.checkMKVersion
                        ):
                            G_CMK_USER, G_CMK_GROUP = getUsrInfo()
                            targetPath.NAGIOS_HOME = (
                                re.sub(r"[/|\\]*$", "", options.nagios_dir)
                                + "/"
                                + "nagiosPlugInForOpenubmc"
                            )
                        else:
                            G_NAGIOS_USER, G_NAGIOS_GROUP = getNagiosUserinfo()
                    install()

                except HelpExcept:
                    parser.print_help()
                except InstallExcept as exc:
                    LOG.error(str(exc.message) + "install fail.")
                    logging.error("Done.")
                else:
                    LOG.info("install success.")
                    logging.error("Done.")
        else:
            parser.print_help()
    else:
        parser.print_help()
